<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/29 0029
 * Time: 14:51
 */

namespace app\common\controller;

/**
 * 工具方法类
 * Class UtilCommon
 * @package app\common\controller
 */
class UtilCommon{
    /**
     * 生成7位邀请码
     * @param int $length
     * @return string
     */
    function GetRandStr( $length = 7 ){
        // 密码字符集，可任意添加你需要的字符
        $chars = array(
//            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
//            'i', 'j', 'k', 'l','m', 'n', 'o', 'p', 'q', 'r', 's',
//            't', 'u', 'v', 'w', 'x', 'y','z', 'A', 'B', 'C', 'D',
//            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L','M', 'N', 'O',
//            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y','Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        $randStr = '';
        for($i = 0; $i < $length; $i++){
            // 将 $length 个数组元素连接成字符串
            $rand = rand(0,count($chars)-1);
            $temp = $chars[$rand];
            $randStr .= $temp;
            unset($chars[$rand]);
            $chars = array_values($chars);
        }
        return $randStr;
    }

    /**
     * 生成不重复的账单号
     * 前缀 + 毫秒时间戳 + 六位随机数
     * @param string $str
     * @return string
     */
    function GetRandStr2($str = ""){
        list($s1, $s2) = explode(' ', microtime());
        $str1 = (float)sprintf('%.0f', (floatval($s1) + floatval($s2)) * 1000);
        $str2 = mt_rand(100000,999999);
        return $str.$str1.$str2;
    }

    /**
     * 获取不重复的六位邀请码 By UserId
     * @return bool
     */
    public function updateInviteCodeByUserId(){
        $UtilCommon = new UtilCommon();
        $userService = new \app\admin\model\User();
        while (true){
            $randStr = $UtilCommon->GetRandStr();
            $where["invite_code"] = $randStr;
            if($userService->where($where)->count() == 0){
                break;
            }
        }
        return $randStr;
    }

    /**
     * 计算两坐标距离(坐标逗号分隔)
     * @param string $center1      坐标1
     * @param string $center2      坐标2
     * @param int $unit            返回类型 1米 2千米
     * @param int $decimal         保留小数位数
     * @return float
     */
    public function getDistance($center1, $center2, $unit = 2, $decimal = 2){
        $EARTH_RADIUS = 6370.996; // 地球半径系数
        $PI = 3.1415926;

        $centerArr1 = explode(',',$center1);
        $centerArr2 = explode(',',$center2);
        // 起点坐标
        $longitude1 = $centerArr1[0];
        $latitude1 =$centerArr1[1];

        // 终点坐标
        $longitude2 = $centerArr2[0];
        $latitude2 = $centerArr2[1];

        $radLat1 = $latitude1 * $PI / 180.0;
        $radLat2 = $latitude2 * $PI / 180.0;

        $radLng1 = $longitude1 * $PI / 180.0;
        $radLng2 = $longitude2 * $PI / 180.0;

        $a = $radLat1 - $radLat2;
        $b = $radLng1 - $radLng2;

        $distance = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));
        $distance = $distance * $EARTH_RADIUS * 1000;

        if ($unit == 2) {
            $distance = $distance / 1000;
        }
        return round($distance, $decimal);
    }
}